DAY5:Delete occurrences of an element if it occurs more than n times


Posted by birdbirdmurmur on 2023-07-18

題目連結:

Delete occurrences of an element if it occurs more than n times

解題思維:

  1. 要有一個儲存數字出現次數的「物件」
  2. 對arr遍歷篩選
  3. 給物件一個屬性
  4. 數字重複就+1 沒有就從1開始
  5. 直到n回傳

實作解法:

function deleteNth(arr,n){
  let count = {};
   return arr.filter( num =>{
     count[num] = (count[num] || 0) +1
     return count[num] <= n
})
}

心得

這題真的超難!
爬完discourse只稍微整理出一點邏輯
我想我目前的程度還不足以挑戰6kyu等級......
連續兩天在6kyu花了太多時間
接下來還是先回頭到7kyu好了


#javascript #Codewars #filter #object







Related Posts

改善肩背疼痛的一些動作

改善肩背疼痛的一些動作

ASP.NET Core Web API 入門教學 - 參數化並過濾接受值

ASP.NET Core Web API 入門教學 - 參數化並過濾接受值

[day-3]布林、undefined、null/字串數字轉換/比較與邏輯運算子

[day-3]布林、undefined、null/字串數字轉換/比較與邏輯運算子


Comments